package com.ruoyi.web.zangchat.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.web.zangchat.domain.ChatUsers;
import com.ruoyi.web.zangchat.domain.mongoBean.MCommentBean;
import com.ruoyi.web.zangchat.domain.mongoBean.MTrendsBean;
import com.ruoyi.web.zangchat.service.IChatUsersService;
import com.ruoyi.web.zangchat.utils.MongoDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.zangchat.mapper.ChatCommentsMapper;
import com.ruoyi.web.zangchat.domain.ChatComments;
import com.ruoyi.web.zangchat.service.IChatCommentsService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 动态评论 服务层实现
 *
 * @author wudi
 * @date 2020-07-22
 */
@Service
public class ChatCommentsServiceImpl extends ServiceImpl<ChatCommentsMapper, ChatComments> implements IChatCommentsService
{
    @Autowired
    private MongoDBUtil mongoDBUtil;
    @Autowired
    private  IChatUsersService chatUsersService;
    @Override
    public List<Map<String, Object>> chatCommentsList(ChatComments chatComments) {
        List<Map<String, Object>> resultList=new ArrayList<>();
        Long trendsId = chatComments.getTrendsId();
        if (trendsId==null){
            return resultList;
        }
        MTrendsBean mTrendsBean = mongoDBUtil.findByTrendsId(Integer.parseInt(trendsId+""));

        List<MCommentBean> commentList = mTrendsBean.getComment();
        if (!commentList.isEmpty()){
            for (MCommentBean commentBean : commentList) {
                Map<String, Object> chatCommentsMap=new HashMap<>();
                chatCommentsMap.put("trendsId",commentBean.getTrendsId());
                chatCommentsMap.put("commentContent",commentBean.getContent());
                chatCommentsMap.put("commentId",commentBean.getToCommentId());
                chatCommentsMap.put("createTime",commentBean.getCreateTime());
                Long userId = commentBean.getUserId();
                ChatUsers chatUsers = chatUsersService.getBaseMapper().selectById(userId);
                String userPhone = chatUsers.getUserPhone();
                chatCommentsMap.put("userPhone",userPhone);
                Long toCommentId=commentBean.getToCommentId();
                if (toCommentId!=0){
                    ChatComments toChatComments = baseMapper.selectById(toCommentId);
                    Long toUserId = toChatComments.getUserId();
                    ChatUsers toUhatUsers = chatUsersService.getBaseMapper().selectById(toUserId);
                    String toUserPhone = toUhatUsers.getUserPhone();
                    chatCommentsMap.put("toUserPhone",toUserPhone);
                }

                resultList.add(chatCommentsMap);
            }

        }
        return resultList;
    }
}